package it.polimi.TravelDream.web.beans;

import it.polimi.TravelDream.ejb.dtos.ExcursionDTO;
import it.polimi.TravelDream.ejb.searchManager.ExcursionMgr;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "ExcursionMgr")
@SessionScoped
public class ExcursionMgrBean {

	@EJB
	private ExcursionMgr excursionManager;
	
	private List <ExcursionDTO> currentList;
	private ExcursionDTO current;
	private int index;
	
	public ExcursionMgrBean(){
		this.currentList = new ArrayList<ExcursionDTO>();
		this.current = new ExcursionDTO();
	}

	public String search(){
		excursionManager.search();
		this.currentList = excursionManager.getResultList();
		return "search excursion results?faces-redirect=true";
	}

	public ExcursionMgr getExcursionManager() {
		return excursionManager;
	}

	public void setExcursionManager(ExcursionMgr excursionManager) {
		this.excursionManager = excursionManager;
	}

	public List<ExcursionDTO> getCurrentList() {
		return currentList;
	}

	public void setCurrentList(List<ExcursionDTO> currentList) {
		this.currentList = currentList;
	}

	public ExcursionDTO getCurrent() {
		return current;
	}

	public void setCurrent(ExcursionDTO current) {
		this.current = current;
	}
	
	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public String searchAll() {
		excursionManager.searchAll();
		this.currentList = excursionManager.getResultList();
		return "search excursion results?faces-redirect=true";
	}
	
	public String goToUpdate(ExcursionDTO excursion) {
		this.current = excursion;
		this.index = this.currentList.indexOf(excursion);
		return "update excursion?faces-redirect=true";
	}
	
	public String update() {
		excursionManager.update(current, index);
		return "search excursion results?faces-redirect=true";
	}
}
